<?php
class RegionAction extends BaseAction {
    public function index() {
        $con['type'] = '1';
        $provinces = D('Region')->where($con)->select();
        $this->assign("provinces",$provinces);
        $this->assign("title",'省直辖市');
        $this->display("Public:region");
    }
    public function city() {
        $condition->parentId = $_GET['id'];
        $city  = D("Region")->where($condition)->select();
        $this->assign("citys",$city);
        $this->assign("title",'城市');
        $this->display("Public:region");
    }
    public function region() {
        $condition->parentId = $_GET['id'];
        $region =  D("Region")->where($condition)->select();
        $this->assign("regions",$region);
        $this->assign("title",'地区');
        $this->display("Public:region");
    }

    public function open() {
        $data['id'] = $_GET['id'];
        $data['open'] = '1';
        if(D("Region")->save($data)) {
            $this->success("修改成功！");
        }else {
            $this->error("资料无改变或修改失败！");
        }
    }

    public function close() {
        $data['id'] = $_GET['id'];
        $data['open'] = '0';
        if(D("Region")->save($data)) {
            $this->success("修改成功！");
        }else {
            $this->error("资料无改变或修改失败！");
        }
    }
   public function batch()
    {
        $act = $_REQUEST['act'];

        if($act == 'open'){
            unset($_POST['act']);
            foreach ($_POST['id'] as $key => $value) {
                $region = D("Region");
                $data['id'] = $value;
                $data['open'] = '1';
                $region->save($data);
                F('region',NULL);
            }
             $this->success("修改成功！");
        }elseif($act == 'pinyin'){
            unset($_POST['act']);
            include COMMON_PATH.'zh2py.php';

            $regions = D("Region")->select();
            foreach ($regions as  $value) {
                $pinyin = getPinyin($value['name']);
                $con['id'] = $value['id'];
                D("Region")->where($con)->setField('pinyin',$pinyin);
                F('region',NULL);
            }
             $this->success("生成拼音成功！");
        } else {
                 $this->_batch();
        }

    }
    
}
?>
